package com.xhsj.user.hr.process.hrBecomeRegular.web;

import com.xhsj.user.hr.aspect.OperationPointCut;
import com.xhsj.user.hr.process.dto.hrBecomeRegular.BecomeRegularHandleAuditDto;
import com.xhsj.user.hr.process.dto.hrBecomeRegular.MyBecomeRegularApplyListDto;
import com.xhsj.user.hr.process.dto.hrBecomeRegular.RunBecomeRegularApplyListDto;
import com.xhsj.user.hr.process.dto.hrBecomeRegular.StartBecomeRegularApplyDto;
import com.xhsj.user.hr.process.dto.processDefinition.HandleAuditDto;
import com.xhsj.user.hr.process.hrBecomeRegular.query.*;
import com.xhsj.user.hr.process.message.service.TbProjectMessageService;
import com.xhsj.user.hr.process.query.ApprovalQuery;
import com.xhsj.user.utils.Message;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.ApiOperation;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.*;
import java.util.concurrent.CompletableFuture;

import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;


import com.xhsj.user.hr.process.hrBecomeRegular.service.TbHrApprovalBecomeRegularApplyService;
import com.xhsj.user.hr.process.hrBecomeRegular.entity.TbHrApprovalBecomeRegularApply;

import javax.validation.Valid;


/**
 * <p>
 * hr流程_转正申请表 前端控制器
 * </p>
 *
 * @author liuqinghua
 * @since Sep 10, 2020 3:48:00 PM
 */
@Slf4j
@Api(tags = "hr流程_转正申请表")
@RestController
@RequestMapping("/hrBecomeRegular/tbHrApprovalBecomeRegularApply")
public class TbHrApprovalBecomeRegularApplyController {

	@Autowired
    public TbHrApprovalBecomeRegularApplyService service;

	// 项目消息 服务实现类
	@Autowired
	public TbProjectMessageService tbProjectMessageService;


	/**
	 * 保存/修改
	 * @author liuqinghua
	 * @date Sep 10, 2020 3:48:00 PM
	 */
	@ApiOperation(value = "hr流程_转正申请表,保存/修改", notes = "hr流程_转正申请表,保存和修改方法")
	@RequestMapping(value = "/save", method = RequestMethod.POST)
    @PreAuthorize("hasAuthority('tb:hr:approval:become:regular:apply:edit')")
	public Message save(@RequestBody TbHrApprovalBecomeRegularApply data){
    	try {
    		service.save(data);
			return Message.success("成功");
		} catch (Exception e) {
			log.error("保存失败:{}",e);
        	return Message.error(1,"失败");
		}

	}

	/**
	 * 通过id删除数据
	 * @author liuqinghua
	 * @date Sep 10, 2020 3:48:00 PM
	 */
	@ApiOperation(value = "hr流程_转正申请表删除", notes = "通过id删除hr流程_转正申请表信息。")
	@RequestMapping(value = "/delete", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('tb:hr:approval:become:regular:apply:delete')")
	public Message delete(@RequestParam(value="id") Long id) {
		try {
			service.removeById(id);
        	return Message.success("成功");
		} catch (Exception e) {
			log.error("删除失败:{}",e);
            return Message.error(1,"失败");
		}

	}

	/**
	 * 批量删除
	 * @author liuqinghua
	 * @date Sep 10, 2020 3:48:00 PM
	 */
	@ApiOperation(value = "批量删除", notes = "批量删除。")
	@RequestMapping(value = "/batchDelete", method = RequestMethod.POST)
	@PreAuthorize("hasAuthority('tb:hr:approval:become:regular:apply:delete')")
	public Message batchDelete(@RequestBody List<Long> list) {
		try {
			service.removeByIds(list);
    		return Message.success("成功");
		} catch (Exception e) {
    		log.error("删除失败:{}",e);
    		return Message.error(1,"失败");
		}
    }

    /**
    * 分页查询数据
    * @author liuqinghua
    * @date Sep 10, 2020 3:48:00 PM
    */
	@ApiOperation(value = "分页查询hr流程_转正申请表list", notes = "带参数查询,{\"page\": 1,\"size\": 5,......}。支持模糊查询")
	@RequestMapping(value="/findList",method = RequestMethod.POST)
    @PreAuthorize("hasAuthority('tb:hr:approval:become:regular:apply:view')")
	public Message findList(@RequestBody Map<String,Object> data) {
        try {
        	IPage<TbHrApprovalBecomeRegularApply> selectPage = service.selectPage(data);
    		return Message.success("成功",selectPage.getRecords(),selectPage.getTotal(),selectPage.getPages());
        } catch (Exception e) {
			log.error("查询异常:{}",e);
    		return Message.error(1,"失败");
        }
	}

    /**
    * 查询所有数据
    * @author liuqinghua
    * @date Sep 10, 2020 3:48:00 PM
    */
	@ApiOperation(value = "查询hr流程_转正申请表所有数据list", notes = "查询hr流程_转正申请表所有数据list。")
	@RequestMapping(value="/findAll",method = RequestMethod.POST)
    @PreAuthorize("hasAuthority('tb:hr:approval:become:regular:apply:view')")
	public Message findAll(){
        return Message.success("成功",service.selectAll());
	}

    /**
    * 通过id获取实体类
    * @author liuqinghua
    * @date Sep 10, 2020 3:48:00 PM
    */
	@ApiOperation(value = "通过id获取hr流程_转正申请表实体类", notes = "通过id获取hr流程_转正申请表实体类。")
	@RequestMapping(value="/getById",method = RequestMethod.GET)
	public Message getById(@RequestParam(value="id") Long id) {
    	return Message.success("成功",service.getById(id));
	}

    /**
    * 通过id查询详细信息
    * @author liuqinghua
    * @date Sep 10, 2020 3:48:00 PM
    */
	@ApiOperation(value = "通过id获取实体类详细信息", notes = "通过id获取实体类详细信息。")
	@RequestMapping(value="/detail",method = RequestMethod.GET)
	public Message detail(@RequestParam(value="id") Long id) {
    	return Message.success("成功",service.getById(id));
	}

	/**
	 * 获取用户要转正申请得数据
	 * @author liuqinghua
	 * @date Sep 10, 2020 3:48:00 PM
	 */
	@ApiOperation(value = "获取用户要转正申请得数据", notes = "获取用户要转正申请得数据")
	@RequestMapping(value="/getBecomeRegularData",method = RequestMethod.GET)
	public Message getBecomeRegularData(@RequestParam(value="userId") Long userId) {
		return Message.success("成功",service.getBecomeRegularData(userId));
	}

	/**
	 * 转正申请开始流程
	 * @author liuqinghua
	 * @date Sep 10, 2020 3:48:00 PM
	 */
	@ApiOperation(value = "转正申请开始流程", notes = "转正申请开始流程")
	@RequestMapping(value="/startBecomeRegularApply",method = RequestMethod.POST)
	@OperationPointCut(module = "HR模块",operation = "转正申请开始流程")
	public Message startBecomeRegularApply(@RequestBody StartBecomeRegularApplyDto data) throws Exception {
		try {
			ApprovalQuery approvalQuery = service.startBecomeRegularApply(data);

			//保存消息
			CompletableFuture<Void> f = CompletableFuture.runAsync(() -> {
				tbProjectMessageService.sendMessageCommon(approvalQuery);
			});
			return Message.success("成功");
		} catch (Exception e) {
			log.error("出错",e);
			return Message.error(1,e.getMessage());
		}


	}



	/**
	 * 转正流程审批
	 * @author liuqinghua
	 * @date Sep 1, 2020 10:07:11 AM
	 */
	@ApiOperation(value = "转正流程审批", notes = "转正流程审批")
	@PostMapping(value="/approvalBecomeRegularApply")
	@OperationPointCut(module = "HR模块",operation = "转正流程审批")
	public Message approvalBecomeRegularApply(@RequestBody @Valid BecomeRegularHandleAuditDto data, BindingResult bindingResult) throws Exception {
		if (bindingResult.hasErrors()) {
			log.info(bindingResult.getFieldError().getDefaultMessage());
			return Message.error(1,bindingResult.getFieldError().getDefaultMessage());
		}
		ApprovalQuery approvalQuery = service.approvalBecomeRegularApply(data);
		//保存消息
		CompletableFuture<Void> f = CompletableFuture.runAsync(() -> {
			tbProjectMessageService.sendMessageCommon(approvalQuery);
		});
		return Message.success("成功");
	}

	/**
	 * 转正流程重新提交
	 * @author liuqinghua
	 * @date Sep 1, 2020 10:07:11 AM
	 */
	@ApiOperation(value = "转正流程重新提交", notes = "转正流程重新提交")
	@PostMapping(value="/returnBecomeRegularApply")
	@OperationPointCut(module = "HR模块",operation = "转正流程重新提交")
	public Message returnBecomeRegularApply(@RequestBody @Valid BecomeRegularHandleAuditDto data, BindingResult bindingResult) throws Exception {
		if (bindingResult.hasErrors()) {
			log.info(bindingResult.getFieldError().getDefaultMessage());
			return Message.error(1,bindingResult.getFieldError().getDefaultMessage());
		}
		ApprovalQuery approvalQuery = service.returnBecomeRegularApply(data);
		//保存消息
		CompletableFuture<Void> f = CompletableFuture.runAsync(() -> {
			tbProjectMessageService.sendMessageCommon(approvalQuery);
		});
		return Message.success("成功");
	}

	/**
	 * 转正流程作废
	 * @author liuqinghua
	 * @date Sep 1, 2020 10:07:11 AM
	 */
	@ApiOperation(value = "转正流程作废", notes = "转正流程作废")
	@PostMapping(value="/approvalBecomeRegularToVoidApply")
	@OperationPointCut(module = "HR模块",operation = "转正流程作废")
	public Message approvalEntryToVoidApply(@RequestBody @Valid HandleAuditDto data, BindingResult bindingResult) throws Exception {
		if (bindingResult.hasErrors()) {
			log.info(bindingResult.getFieldError().getDefaultMessage());
			return Message.error(1,bindingResult.getFieldError().getDefaultMessage());
		}
		ApprovalQuery approvalQuery = service.approvalBecomeRegularToVoidApply(data);
		return Message.success("成功");
	}

	/**
	 * 我的转正申请列表
	 * @author liuqinghua
	 * @date Sep 1, 2020 10:07:11 AM
	 */
	@ApiOperation(value = "我的转正申请列表", notes = "我的转正申请列表")
	@PostMapping(value="/myBecomeRegularApplyList")
	public Message myBecomeRegularApplyList(@RequestBody @Valid MyBecomeRegularApplyListDto data, BindingResult bindingResult) throws Exception {
		log.info("我的转正申请列表参数{}", data);
		if (bindingResult.hasErrors()) {
			log.info(bindingResult.getFieldError().getDefaultMessage());
			return Message.error(1,bindingResult.getFieldError().getDefaultMessage());
		}
		IPage<MyBecomeRegularApplyListQuery> myBecomeRegularApplyListQueryPage = service.myBecomeRegularApplyList(data);
		return Message.success("成功",myBecomeRegularApplyListQueryPage.getRecords(),myBecomeRegularApplyListQueryPage.getTotal(),myBecomeRegularApplyListQueryPage.getPages());
	}

	/**
	 * 待审核转正申请列表
	 * @author liuqinghua
	 * @date Sep 1, 2020 10:07:11 AM
	 */
	@ApiOperation(value = "待审核转正申请列表", notes = "待审核转正申请列表")
	@PostMapping(value="/runBecomeRegularApplyList")
	public Message runBecomeRegularApplyList(@RequestBody @Valid RunBecomeRegularApplyListDto data, BindingResult bindingResult) throws Exception {
		log.info("待审核转正申请列表参数{}", data);
		if (bindingResult.hasErrors()) {
			log.info(bindingResult.getFieldError().getDefaultMessage());
			return Message.error(1,bindingResult.getFieldError().getDefaultMessage());
		}
		IPage<RunBecomeRegularApplyListQuery> myBecomeRegularApplyListQueryPage = service.runBecomeRegularyApplyList(data);
		return Message.success("成功",myBecomeRegularApplyListQueryPage.getRecords(),myBecomeRegularApplyListQueryPage.getTotal(),myBecomeRegularApplyListQueryPage.getPages());
	}

	/**
	 * 已审核转正申请列表
	 * @author liuqinghua
	 * @date Sep 1, 2020 10:07:11 AM
	 */
	@ApiOperation(value = "已审核转正申请列表", notes = "已审核转正申请列表")
	@PostMapping(value="/completedBecomeRegularApplyList")
	public Message completedBecomeRegularApplyList(@RequestBody @Valid RunBecomeRegularApplyListDto data, BindingResult bindingResult) throws Exception {
		log.info("已审核转正申请列表参数{}", data);
		if (bindingResult.hasErrors()) {
			log.info(bindingResult.getFieldError().getDefaultMessage());
			return Message.error(1,bindingResult.getFieldError().getDefaultMessage());
		}
		IPage<CompletedBecomeRegularApplyQuery> completedBecomeRegularApplyList = service.completedBecomeRegularApplyList(data);
		return Message.success("成功",completedBecomeRegularApplyList.getRecords(),completedBecomeRegularApplyList.getTotal(),completedBecomeRegularApplyList.getPages());
	}

	/**
	 * 我的转正申请详细信息
	 * @author liuqinghua
	 * @date Sep 1, 2020 10:07:11 AM
	 */
	@ApiOperation(value = "我的转正申请详细信息", notes = "我的转正申请详细信息")
	@GetMapping(value="/myBecomeRegularApplyDetailedList")
	public Message myBecomeRegularApplyDetailedList(@RequestParam(value = "dataId") Long dataId) throws Exception {
		log.info("我的转正申请详细信息参数{}", dataId);
		MyBecomeRegularDetailedListQuery myLeaveApplyListQuery = service.myBecomeRegularApplyDetailedList(dataId);
		return Message.success("成功",myLeaveApplyListQuery);
	}

    /**
     * 我的转正申请详细信息之评审分数
     * @author liuqinghua
     * @date Sep 1, 2020 10:07:11 AM
     */
    @ApiOperation(value = "我的转正申请详细信息评审分数", notes = "我的转正申请详细信息评审分数")
    @GetMapping(value="/myBecomeRegularApplyDetailedListScore")
    public Message myBecomeRegularApplyDetailedListScore(@RequestParam(value = "dataId") Long dataId) throws Exception {
        log.info("我的转正申请详细信息评审分数参数{}", dataId);
        List<MyBecomeRegularApplyDetailedListScoreQuery> myBecomeRegularApplyDetailedListScoreQueries = service.myBecomeRegularApplyDetailedListScore(dataId);
        return Message.success("成功",myBecomeRegularApplyDetailedListScoreQueries);
    }

	/**
	 * 全部转正申请列表
	 * @author liuqinghua
	 * @date Sep 1, 2020 10:07:11 AM
	 */
	@ApiOperation(value = "全部转正申请列表", notes = "全部转正申请列表")
	@PostMapping(value="/allBecomeRegularApplyList")
	@PreAuthorize("hasAuthority('tb:hr:approval:become:regular:apply:all:view')")
	public Message allBecomeRegularApplyList(@RequestBody @Valid MyBecomeRegularApplyListDto data, BindingResult bindingResult) throws Exception {
		log.info("全部转正申请列表参数{}", data);
        IPage<MyBecomeRegularApplyListQuery> myBecomeRegularApplyListQueryIPage = service.allBecomeRegularApplyList(data);
        return Message.success("成功",myBecomeRegularApplyListQueryIPage.getRecords(),myBecomeRegularApplyListQueryIPage.getTotal(),myBecomeRegularApplyListQueryIPage.getPages());
	}


}

